/**
 * fileName:  FruitCategoryReader.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-5-31 上午11:46:29
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-5-31          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.read;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.store.DatabaseReader;
import org.cloudland.commercial.masterdata.store.entity.CategoryEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.FruitCategoryEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.FruitCategoryMapper;
import org.springframework.stereotype.Service;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  FruitCategoryReader
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Service
public class FruitCategoryReader implements DatabaseReader<CategoryEntity> {

	@Resource(name = "fruitCategoryMapper")
	private FruitCategoryMapper categoryMapper;
	
	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public FruitCategoryReader() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * <pre>
	 * 查询根节点
	 * </pre>
	 *
	 * @return   
	 */
	public List<CategoryEntity> findCategoryRoot() {
		return findChildCategoryById("");
	}
	
	/**
	 * <pre>
	 * 根据编号查询分类
	 * </pre>
	 *
	 * @param categoryId
	 * @return   
	 */
	public CategoryEntity findCategoryById(String categoryId) {
		FruitCategoryEntity daoEntity = categoryMapper.findById(categoryId);
		
		CategoryEntity entity = null;
		if (null != daoEntity) {
			entity = new CategoryEntity(daoEntity.getId(), daoEntity.getName(), (int) daoEntity.getSort(), daoEntity.getParentId());
		}
		return entity;		
	}
	
	/**
	 * <pre>
	 * 根据分类编号查询子分类
	 * </pre>
	 *
	 * @param categoryId
	 * @return   
	 */
	public List<CategoryEntity> findChildCategoryById(String categoryId) {
		
		List<FruitCategoryEntity> arrayChild = categoryMapper.findChildById(categoryId);
		if (null == arrayChild || arrayChild.size() < 1) {
			return null;
		}
		
		List<CategoryEntity> resultArray = new ArrayList<CategoryEntity>(arrayChild.size());
		for (FruitCategoryEntity daoEntity : arrayChild) {
			resultArray.add(new CategoryEntity(daoEntity.getId(), daoEntity.getName(), (int) daoEntity.getSort(), daoEntity.getParentId()));
		}
		
		return resultArray;		
	}

}
